import store from '@/store/index'
import request from '@/utils/request'

export function getData(url, params) {
  // 参数
  return request({
    url: url, method: 'get', params
  })
}

export function getDataByToken(url, params) {
  params['token'] = store.state.user.token
  // 参数
  return request({
    url: url, method: 'get', params
  })
}

export function postData(url, dataJson, token) {
  token = store.state.user.token
  const date = JSON.stringify(dataJson)
  // date = encodeURIComponent(date)
  const params = { 'token': token || store.state.user.token, 'data': date }
  // 参数
  return request({
    url: url, method: 'post', data: params
  })
}

export function postDataByToken(url, dataJson) {
  const token = store.state.user.token
  const date = JSON.stringify(dataJson)
  const params = { 'token': token, 'data': date }
  // 参数
  return request({
    url: url, method: 'post', data: params
  })
}

// ========== ========== ========== 获取界面样式 ========== ========== ==========

// List界面
export function getListFrame(model, entity) {
  // 如果是测试模式，重设请求地址
  const tModel = store.getters['setting/getModel']
  if (tModel === '1') {
    const params = { 'token': store.getters['user/getToken'] }
    // 参数
    return request({
      url: model + '/' + entity + '/getListFrame', method: 'get', params
    })
  } else {
    return getData('/kvms/viewModel/getListViewFrame', {
      modelCode: model, entityCode: entity
    })
  }
}

// Info界面
export function getInfoFrame(model, entity) {
  // 如果是测试模式，重设请求地址
  const tModel = store.getters['setting/getModel']
  if (tModel === '1') {
    const params = { 'token': store.getters['user/getToken'] }
    // 参数
    return request({
      url: model + '/' + entity + '/getInfoFrame', method: 'get', params
    })
  } else {
    return getData('/kvms/viewModel/getInfoViewFrame', {
      modelCode: model, entityCode: entity
    })
  }
}

// Search界面
export function getSearchFrame(model, entity) {
  // 如果是测试模式，重设请求地址
  const tModel = store.getters['setting/getModel']
  if (tModel === '1') {
    const params = { 'token': store.getters['user/getToken'] }
    // 参数
    return request({
      url: model + '/' + entity + '/getSearchFrame', method: 'get', params
    })
  } else {
    return getData('/kvms/viewModel/getSearchViewFrame', {
      modelCode: model, entityCode: entity
    })
  }
}

// Search界面
export function getComListViewFrame(model, entity) {
  // 如果是测试模式，重设请求地址
  const tModel = store.getters['setting/getModel']
  if (tModel === '1') {
    const params = { 'token': store.getters['user/getToken'] }
    // 参数
    return request({
      url: model + '/' + entity + '/getListFrame', method: 'get', params
    })
  } else {
    return getData('/kvms/viewModel/getComListViewFrame', {
      modelCode: model, entityCode: entity
    })
  }
}

// ========== ========== ========== 操作Api接口 ========== ========== =========
export function save(url, dataJson) {
  let date = JSON.stringify(dataJson)
  date = encodeURIComponent(date)
  const params = { 'token': store.getters['user/getToken'], 'data': date }
  // 参数
  return request({
    url: url + '/do/save', method: 'post', data: params
  })
}

export function update(url, dataJson) {
  let date = JSON.stringify(dataJson)
  date = encodeURIComponent(date)
  const params = { 'token': store.getters['user/getToken'], 'data': date }
  // 参数
  return request({
    url: url + '/do/update', method: 'post', data: params
  })
}

export function remove(url, id) {
  const params = { 'token': store.getters['user/getToken'], 'id': id }
  // 参数
  return request({
    url: url + '/do/delete', method: 'get', params
  })
}

// ========== ========== ========== 查询Api接口 ========== ========== =========
export function getById(url, id) {
  const params = {}
  params['token'] = store.getters['user/getToken']
  params['id'] = id
  // 参数
  return request({
    url: url + '/get/findById', method: 'get', params
  })
}

/**
 * 通过code查询
 * @param url
 * @param code
 * @returns {AxiosPromise}
 */
export function getByCode(url, code) {
  const params = { 'token': store.getters['user/getToken'], 'code': code }
  return request({
    url: url + '/get/findByCode', method: 'get', params
  })
}

export function findAll(url, searchDataJson, orderDataJson) {
  let searchData = JSON.stringify(searchDataJson)
  searchData = encodeURIComponent(searchData)

  let orderData = JSON.stringify(orderDataJson)
  orderData = encodeURIComponent(orderData)

  const params = {}
  params['token'] = store.getters['user/getToken']
  params['searchData'] = searchData
  params['orderData'] = orderData
  // 参数
  return request({
    url: url + '/findMap/all', method: 'get', params
  })
}

export function findPage(url, searchDataJson, pageSize, pageNo) {
  const dataJson = {}
  dataJson['searchData'] = searchDataJson
  dataJson['pageSize'] = pageSize
  dataJson['pageNo'] = pageNo
  const date = JSON.stringify(dataJson)
  const params = { 'token': store.getters['user/getToken'], 'data': date }

  // 参数
  return request({
    url: url + '/find/findPagination', method: 'post', data: params
  })
}

export function findCount(model, entity, searchDataJson) {
  const searchData = JSON.stringify(searchDataJson)
  // searchData = encodeURIComponent(searchData)
  const params = {}
  params['token'] = store.getters['user/getToken']
  params['searchData'] = searchData
  // 参数
  return request({
    url: model + '/' + entity + '/find/count', method: 'get', params
  })
}

export function findPopup(url, searchDataJson, pageSign, pageSize, pageNo) {
  const dataJson = {}
  dataJson['searchData'] = searchDataJson
  if (pageSign) {
    dataJson['pageSize'] = pageSize
    dataJson['pageNo'] = pageNo
  }
  const date = JSON.stringify(dataJson)
  const params = { 'token': store.getters['user/getToken'], 'data': date }
  // 参数
  return request({
    url: url, method: 'post', data: params
  })
}
